package com.cardone.common.util;

import java.io.IOException;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import lombok.extern.slf4j.Slf4j;

import org.apache.commons.io.IOUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.util.CollectionUtils;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

/**
 * web工具类
 *
 * @author yaohaitao
 *
 */
@Slf4j
public class WebUtils {
	/**
	 * 获取bean关于json
	 *
	 * @param mappedClass
	 *          映射类型
	 *
	 * @return bean @
	 */
	public static <T> T getBeanForJson(final Class<T> mappedClass) {
		final HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest();

		return WebUtils.getBeanForJson(mappedClass, request);
	}

	/**
	 * 获取bean关于json
	 *
	 * @param mappedClass
	 *          映射类型
	 * @param request
	 *
	 * @return bean @
	 */
	public static <T> T getBeanForJson(final Class<T> mappedClass, final HttpServletRequest request) {
		final String jsonString = WebUtils.getJsonString(request);

		return MapperUtils.getBeanForJson(mappedClass, jsonString);
	}

	/**
	 * 获取bean关于json
	 *
	 * @param mappedClass
	 *          映射类型
	 * @param paramName
	 *          参数名称
	 *
	 * @return bean @
	 */
	public static <T> T getBeanForJson(final Class<T> mappedClass, final String paramName) {
		final HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest();

		return WebUtils.getBeanForJson(mappedClass, paramName, request);
	}

	/**
	 * 获取bean关于json
	 *
	 * @param mappedClass
	 *          映射类型
	 * @param paramName
	 *          参数名称
	 * @param request
	 *
	 * @return bean @
	 */
	public static <T> T getBeanForJson(final Class<T> mappedClass, final String paramName, final HttpServletRequest request) {
		final String jsonString = WebUtils.getJsonString(request);

		return MapperUtils.getBeanForJson(mappedClass, paramName, jsonString);
	}

	/**
	 * 获取json字符串
	 *
	 * @return json字符串
	 */
	public static String getJsonString() {
		final HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest();

		return WebUtils.getJsonString(request);
	}

	/**
	 * 获取json字符串
	 *
	 * @param request
	 * @return json字符串
	 */
	public static String getJsonString(final HttpServletRequest request) {
		String jsonString = null;

		try {
			jsonString = IOUtils.toString(request.getInputStream(), request.getCharacterEncoding());
		} catch (final IOException e) {
			WebUtils.log.error(e.getMessage(), e);
		}

		if (StringUtils.isNotBlank(jsonString)) {
			return jsonString;
		}

		final Map<String, Object> paramNameMap = org.springframework.web.util.WebUtils.getParametersStartingWith(request, null);

		if (!CollectionUtils.isEmpty(paramNameMap)) {
			return MapperUtils.toJson(paramNameMap);
		}

		if (StringUtils.isNotBlank(jsonString)) {
			return jsonString;
		}

		return StringUtils.EMPTY;
	}

	/**
	 * 获取键值对
	 *
	 * @return 键值对 @
	 */
	public static Map<String, Object> getMapForJson() {
		final HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest();

		return WebUtils.getMapForJson(request);
	}

	/**
	 * 获取键值对
	 *
	 * @param request
	 * @return 键值对 @
	 */
	public static Map<String, Object> getMapForJson(final HttpServletRequest request) {
		final String jsonString = WebUtils.getJsonString(request);

		return MapperUtils.getMapForJson(jsonString);
	}

	/**
	 * 获取对象
	 *
	 * @param paramName
	 *          参数名称
	 * @return 对象 @
	 */
	public static Object getObjectForJson(final String paramName) {
		final HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest();

		return WebUtils.getObjectForJson(paramName, request);
	}

	/**
	 * 获取对象
	 *
	 * @param paramName
	 *          参数名称
	 * @param request
	 * @return 对象 @
	 */
	public static Object getObjectForJson(final String paramName, final HttpServletRequest request) {
		final String jsonString = WebUtils.getJsonString(request);

		return MapperUtils.getObjectForJson(paramName, jsonString);
	}
}
